const schedule = require("node-schedule");
let spawn = require("child_process").spawn;

/*
https://www.npmjs.com/package/node-schedule

*    *    *    *    *    *
┬    ┬    ┬    ┬    ┬    ┬
│    │    │    │    │    │
│    │    │    │    │    └ day of week (0 - 7) (0 or 7 is Sun)
│    │    │    │    └───── month (1 - 12)
│    │    │    └────────── day of month (1 - 31)
│    │    └─────────────── hour (0 - 23)
│    └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, OPTIONAL)
*/

const job = schedule.scheduleJob(
  {
    hour: 6,
    minute: 1,
    date: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31],
  },
  function () {
    spawn("./test.js").stdout.on("data", function (data) {
      console.log(data.toString());
    });
  }
);

job.on("scheduled", () => console.log("----- job is cheduled -----"));

process.on("SIGINT", function () {
  schedule.gracefulShutdown().then(() => process.exit(0));
});
